﻿using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.Supplier;
using ProduceBasicData.Domain.Material;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Supplier
{
    /// <summary>
    /// 供应商信息删除处理
    /// </summary>
    public class DeleteSupplierHandler : IRequestHandler<DeleteSupplierCommand, ApiResult<int>>
    {
        private readonly IBaseRespository<SupplierModel> supplierRepo;

        public DeleteSupplierHandler(IBaseRespository<SupplierModel> supplierRepo)
        {
            this.supplierRepo = supplierRepo;
        }

        public async Task<ApiResult<int>> Handle(DeleteSupplierCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res=new ApiResult<int>();

            var supplier = await supplierRepo.GetModelAsync(request.Id);
            if (supplier == null)
            {
                res.Msg = "您要删除的数据不存在";
                res.Code = ApiEnum.Failed;
                return res;
            }
            supplier.Isdel = true;



            var r=await supplierRepo.Update(supplier);
            if (r>0)
            {
                res.Data=r;
                res.Msg = "删除成功";
                res.Code=ApiEnum.Success;
            }
            else
            {
                res.Msg = "删除失败";
                res.Code = ApiEnum.Failed;
            }
            return res;
        }
    }
}
